package cn.xerllent.quartz.config;

import cn.xerllent.quartz.entity.QuartzJob;
import cn.xerllent.quartz.job.TestJob;
import cn.xerllent.quartz.job.TestJob2;
import cn.xerllent.quartz.service.QuartzJobService;
import cn.xerllent.quartz.utils.QuartzManage;
import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 */
@Slf4j
@Component
public class JobRunner implements ApplicationRunner {

    private final QuartzJobService quartzJobService;
    private final QuartzManage quartzManage;

    public JobRunner(QuartzJobService quartzJobService, QuartzManage quartzManage) {
        this.quartzJobService = quartzJobService;
        this.quartzManage = quartzManage;
    }

    /**
     * 项目启动时重新激活启用的定时任务
     * @param applicationArguments /
     */
    @Override
    public void run(ApplicationArguments applicationArguments) {
        log.info("--------------------注入定时任务---------------------");
        List<QuartzJob> quartzJobs = quartzJobService.findByEnabled();
        quartzJobs.forEach(quartzManage::addJob);
        
//        QuartzJob TestJob =new QuartzJob();
//        TestJob.setBean("cn.xerllent.quartz.job.TestJob2");
//        TestJob.setCron("0/10 * * * * ?");
//        TestJob.setId(100);
//        TestJob.setEnabled(true);
//        TestJob.setName("测试renwu");
//        TestJob.setParams(new HashMap<String, Object>(){{
//        	put("a","this is a....");
//        	put("b",222222);
//        }});
//        quartzManage.addJob(TestJob);
        log.info("--------------------定时任务注入完成---------------------");
    }
}
